package ca.venasse.pinventory.inventory.command;

import org.axonframework.commandhandling.annotation.CommandHandler;
import org.axonframework.repository.Repository;

import ca.venasse.pinventory.inventory.Inventory;

public class InventoryCommandHandler {

	private Repository<Inventory> repository;

	@CommandHandler
	public void handleInventory(CreateInventory command) {
		Inventory inventory = new Inventory(command.getIdentifier(), command.getName());
		this.repository.add(inventory);
	}

	@CommandHandler
	public void handleLocation(ChangeParentOfLocation command) {
		Inventory inventory = this.repository.load(command.getAggregateIdentifier());
		inventory.changeParentOfLocation(
			command.getParentLocationIdentifier(),
			command.getLocationIdentifier());
	}

	@CommandHandler
	public void handleLocation(CreateLocation command) {
		Inventory inventory = this.repository.load(command.getAggregateIdentifier());
		inventory.createLocation(command.getLocationIdentifier(), command.getName());
	}

	@CommandHandler
	public void handleLocation(CreateChildLocation command) {
		Inventory inventory = this.repository.load(command.getAggregateIdentifier());
		inventory.createChildLocation(
			command.getParentLocationIdentifier(),
			command.getLocationIdentifier(),
			command.getName());
	}

	@CommandHandler
	public void handleLocation(RemoveParentOfLocation command) {
		Inventory inventory = this.repository.load(command.getAggregateIdentifier());
		inventory.removeParentOfLocation(command.getLocationIdentifier());
	}

	@CommandHandler
	public void handlePart(AddPart command) {
		Inventory inventory = this.repository.load(command.getAggregateIdentifier());
		inventory.addPart(
			command.getCategoryIdentifier(),
			command.getPartIdentifier(),
			command.getName());
	}

	@CommandHandler
	public void handlePart(PurchasePart command) {
		Inventory inventory = this.repository.load(command.getAggregateIdentifier());
		inventory.purchasePart(
			command.getPartIdentifier(),
			command.getSupplierName(),
			command.getPartUrl(),
			command.getPurchaseDate(),
			command.getQuantity(),
			command.getUnitCost());
	}

	@CommandHandler
	public void handlePart(RecordLoss command) {
		Inventory inventory = this.repository.load(command.getAggregateIdentifier());
		inventory.recordLoss(command.getPartIdentifier(), command.getQuantity());
	}

}
